/*
 * Service class 
 * Created on 6 juin 2013 ( Time 15:03:49 )
 */

package org.demo.siteemploye.service;

import java.util.List;

import org.demo.siteemploye.bean.Siteemploye;
import org.demo.siteemploye.dao.JpaSiteemployeDAO;
import org.telosys.starterkits.strutsjpa.EntityManagerHelper;
import org.telosys.starterkits.strutsjpa.IServices;
import javax.persistence.PersistenceException;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import org.demo.siteemploye.bean.SiteemployeKey;

public class SiteemployeServices implements IServices<Siteemploye, SiteemployeKey> {

	protected final Logger LOG = LoggerFactory.getLogger(SiteemployeServices.class);

	public Siteemploye load(final SiteemployeKey id) {
		if (LOG.isDebugEnabled()) LOG.debug("load");
		Siteemploye siteemploye;
		try {
			EntityManagerHelper.beginTransaction();
			JpaSiteemployeDAO siteemployeDAO = new JpaSiteemployeDAO();
			siteemploye = siteemployeDAO.findById(id);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return siteemploye ;
	}

	public Siteemploye save(final Siteemploye entity) {
		if (LOG.isDebugEnabled()) LOG.debug("save");
		Siteemploye entityNew;
		try {
			EntityManagerHelper.beginTransaction();
			JpaSiteemployeDAO siteemployeDAO = new JpaSiteemployeDAO();
			entityNew = siteemployeDAO.update(entity);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return entityNew;
	}

	public void delete(final SiteemployeKey id) {
		if (LOG.isDebugEnabled()) LOG.debug("delete");
		try {
			EntityManagerHelper.beginTransaction();
			JpaSiteemployeDAO siteemployeDAO = new JpaSiteemployeDAO();
			siteemployeDAO.delete(id);
			EntityManagerHelper.commitAndCloseEntityManager();	
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
	}

	public List<Siteemploye> search(final Siteemploye siteemploye) {
		if (LOG.isDebugEnabled()) LOG.debug("search");
		List<Siteemploye> liste;
		try {
			EntityManagerHelper.beginTransaction();
			JpaSiteemployeDAO siteemployeDAO = new JpaSiteemployeDAO();
			liste = siteemployeDAO.search(siteemploye);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return liste;
	}

}
